unit oFormGenerator;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, oGeneratorData;

type
  TfrmGenerator = class(TForm)
    rgpType: TRadioGroup;
    gbxMain: TGroupBox;
    txtMultiplier: TLabeledEdit;
    txtConstant: TLabeledEdit;
    chbRandomized: TCheckBox;
    gbxCustom: TGroupBox;
    txtMean: TLabeledEdit;
    txtDispersion: TLabeledEdit;
    txtMinimum: TLabeledEdit;
    txtMaximum: TLabeledEdit;
    txtValue: TLabeledEdit;
    txtProbability: TLabeledEdit;
    btnOK: TButton;
    btnCacnel: TButton;
    procedure txtConstantExit(Sender: TObject);
    procedure txtMultiplierExit(Sender: TObject);
    procedure txtMeanChange(Sender: TObject);
    procedure txtDispersionExit(Sender: TObject);
    procedure txtMinimumExit(Sender: TObject);
    procedure txtMaximumExit(Sender: TObject);
    procedure txtValueExit(Sender: TObject);
    procedure txtProbabilityExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure rgpTypeClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure btnCacnelClick(Sender: TObject);
  private
    { Private declarations }
    aGeneratorData: TGeneratorData;
    aNewGenerator: TGeneratorData;
    procedure checkNumber(paText: TLabeledEdit);
    procedure checkInt(paText: TLabeledEdit);
    procedure checkValue(paText: TLabeledEdit);
    procedure clearForm;
    procedure enableTextsForIndex;
    function getMean: Double;
    function getDispersion: Double;
    function getMin: Double;
    function getMax: Double;
    function getValue: Integer;
    function getProbability: Double;
    function getMultiplier: Double;
    function getConstant: Double;
    function isRandomized: Boolean;
  public
    { Public declarations }
    property Data: TGeneratorData read aGeneratorData;
    procedure Show(paGeneratorData: TGeneratorData); overload;
  end;

var
  frmGenerator: TfrmGenerator;

implementation

{$R *.dfm}

{ TfrmGenerator }

procedure TfrmGenerator.btnCacnelClick(Sender: TObject);
begin
  FreeAndNil(aNewGenerator);
  aNewGenerator := aGeneratorData;
  close;
end;

procedure TfrmGenerator.btnOKClick(Sender: TObject);
begin
  case rgpType.ItemIndex of
    //gamma
    0: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TGammaGeneratorData.create(genGamma,getMultiplier,getConstant,getMean,getDispersion,getMin,getMax,isRandomized);
    end;
    //beta
    1: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TBetaGeneratorData.create(genBeta,getMultiplier,getConstant,getMean,getDispersion,getMin,getMax,isRandomized);
    end;
    //binomial
    2: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TBinomialGeneratorData.create(genBinomial,getValue,getMultiplier,getConstant,getProbability,isRandomized);
    end;
    //discrete
    3: begin
      MessageDlg('sorry, this type is not supported yet',mtInformation,[mbOK],0);
      rgpType.SetFocus;
    end;
    //erlang
    4: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TErlangGeneratorData.create(genErlang,getMultiplier,getConstant,getMean,getDispersion,isRandomized);
    end;
    //exponential
    5: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TExponentialGeneratorData.create(genExponential,getMultiplier,getConstant,getMean,isRandomized);
    end;
    //linear
    6: begin
      MessageDlg('sorry, this type is not supported yet',mtInformation,[mbOK],0);
      rgpType.SetFocus;
    end;
    //normal
    7: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TNormalGeneratorData.create(genNormal,getMultiplier,getConstant,getMean,getDispersion,isRandomized);
    end;
    //poisson
    8: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TPoissonGeneratorData.create(genPoisson,getMultiplier,getConstant,getMean,isRandomized);
    end;
    //uniform
    9: begin
      FreeAndNil(aNewGenerator);
      aNewGenerator := TUniformGeneratorData.create(genUniform,getMultiplier,getConstant,getMin,getMax,isRandomized);
    end;
  end;
  //FreeAndNil(aGeneratorData);
  aGeneratorData := aNewGenerator;
  close;
end;

procedure TfrmGenerator.checkInt(paText: TLabeledEdit);
begin
try
    StrToInt(paText.Text);
  except
    MessageDlg(paText.Text + ' is not a integer',mtError,[mbOK],0);
    paText.SetFocus;
  end;
end;

procedure TfrmGenerator.checkNumber(paText: TLabeledEdit);
begin
  try
    StrToFloat(paText.Text);
  except
    MessageDlg(paText.Text + ' is not a number',mtError,[mbOK],0);
    paText.SetFocus;
  end;
end;

procedure TfrmGenerator.checkValue(paText: TLabeledEdit);
var
  mr: Integer;
begin
  try
    StrToFloat(paText.Text);
    if paText.Text = '0' then
      begin
        mr := MessageDlg('Zero value, commit?',mtWarning,[mbYes,mbNo],0);
        if mr = mrYes then paText.SetFocus;
      end;
  except
    MessageDlg(paText.Text + ' is not a number',mtError,[mbOK],0);
    paText.SetFocus;
  end;
end;

procedure TfrmGenerator.clearForm;
begin
  txtMultiplier.Text := '1';
  txtConstant.Text := '0';
  txtDispersion.Text := '0';
  txtMinimum.Text := '0';
  txtMaximum.Text := '0';
  txtValue.Text := '0';
  txtProbability.Text := '1';
end;

procedure TfrmGenerator.enableTextsForIndex;
begin
  case rgpType.ItemIndex of
    //gamma
    0: begin
      txtMean.Enabled := true;
      txtDispersion.Enabled := true;
      txtMinimum.Enabled := true;
      txtMaximum.Enabled := true;
      txtValue.Enabled := false;
      txtProbability.Enabled := false;
    end;
    //beta
    1: begin
      txtMean.Enabled := true;
      txtDispersion.Enabled := true;
      txtMinimum.Enabled := true;
      txtMaximum.Enabled := true;
      txtValue.Enabled := false;
      txtProbability.Enabled := false;
    end;
    //binomial
    2: begin
      txtMean.Enabled := false;
      txtDispersion.Enabled := false;
      txtMinimum.Enabled := false;
      txtMaximum.Enabled := false;
      txtValue.Enabled := true;
      txtProbability.Enabled := true;
    end;
    //discrete
    3: begin
      MessageDlg('sorry, this type is not supported yet',mtInformation,[mbOK],0);
      rgpType.SetFocus;
    end;
    //erlang
    4: begin
      txtMean.Enabled := true;
      txtDispersion.Enabled := true;
      txtMinimum.Enabled := false;
      txtMaximum.Enabled := false;
      txtValue.Enabled := false;
      txtProbability.Enabled := false;
    end;
    //exponential
    5: begin
      txtMean.Enabled := true;
      txtDispersion.Enabled := false;
      txtMinimum.Enabled := false;
      txtMaximum.Enabled := false;
      txtValue.Enabled := false;
      txtProbability.Enabled := false;
    end;
    //linear
    6: begin
      MessageDlg('sorry, this type is not supported yet',mtInformation,[mbOK],0);
      rgpType.SetFocus;
    end;
    //normal
    7: begin
      txtMean.Enabled := true;
      txtDispersion.Enabled := true;
      txtMinimum.Enabled := false;
      txtMaximum.Enabled := false;
      txtValue.Enabled := false;
      txtProbability.Enabled := false;
    end;
    //poisson
    8: begin
      txtMean.Enabled := true;
      txtDispersion.Enabled := false;
      txtMinimum.Enabled := false;
      txtMaximum.Enabled := false;
      txtValue.Enabled := false;
      txtProbability.Enabled := false;
    end;
    //uniform
    9: begin
      txtMean.Enabled := false;
      txtDispersion.Enabled := false;
      txtMinimum.Enabled := true;
      txtMaximum.Enabled := true;
      txtValue.Enabled := false;
      txtProbability.Enabled := false;
    end;
  end;
end;

procedure TfrmGenerator.FormCreate(Sender: TObject);
begin
  aGeneratorData := nil;
end;

procedure TfrmGenerator.FormDestroy(Sender: TObject);
begin
  aNewGenerator := nil;
  aGeneratorData := nil;
end;

function TfrmGenerator.getConstant: Double;
begin
  result := StrToFloat(txtConstant.Text);
end;

function TfrmGenerator.getDispersion: Double;
begin
  result := StrToFloat(txtDispersion.Text);
end;

function TfrmGenerator.getMax: Double;
begin
  result := StrToFloat(txtMaximum.Text);
end;

function TfrmGenerator.getMean: Double;
begin
  result := StrToFloat(txtMean.Text);
end;

function TfrmGenerator.getMin: Double;
begin
  result := StrToFloat(txtMinimum.Text);
end;

function TfrmGenerator.getMultiplier: Double;
begin
  result := StrToFloat(txtMultiplier.Text);
end;

function TfrmGenerator.getProbability: Double;
begin
  result := StrToFloat(txtProbability.Text);
end;

function TfrmGenerator.getValue: Integer;
begin
  result := StrToInt(txtValue.Text);
end;

function TfrmGenerator.isRandomized: Boolean;
begin
  result := chbRandomized.Checked;
end;

procedure TfrmGenerator.rgpTypeClick(Sender: TObject);
begin
  enableTextsForIndex;
end;

procedure TfrmGenerator.Show(paGeneratorData: TGeneratorData);
begin
  aGeneratorData := paGeneratorData;
  aNewGenerator := nil;
  clearForm;
  if aGeneratorData <> nil then
    begin
      txtMultiplier.Text := FloatToStr(aGeneratorData.Multiplier);
      txtConstant.Text := FloatToStr(aGeneratorData.Constant);
      chbRandomized.Checked := aGeneratorData.Randomized;
      case aGeneratorData.Code of
        genGamma: begin
          rgpType.ItemIndex := 0;
          with aGeneratorData as TGammaGeneratorData do
            begin
              txtMean.Text := FloatToStr(Mean);
              txtDispersion.Text := FloatToStr(Dispersion);
              txtMinimum.Text := FloatToStr(MinX);
              txtMaximum.Text := FloatToStr(MaxX);
            end;
        end;
        genBeta: begin
          rgpType.ItemIndex := 1;
          with aGeneratorData as TBetaGeneratorData do
            begin
              txtMean.Text := FloatToStr(Mean);
              txtDispersion.Text := FloatToStr(Dispersion);
              txtMinimum.Text := FloatToStr(MinX);
              txtMaximum.Text := FloatToStr(MaxX);
            end;
        end;
        genBinomial: begin
          rgpType.ItemIndex := 2;
          with aGeneratorData as TBinomialGeneratorData do
            begin
              txtValue.Text := FloatToStr(Number);
              txtProbability.Text := FloatToStr(Probab);
            end;
        end;
        genDiscrete: begin
          MessageDlg('sorry, this type is not supported yet',mtInformation,[mbOK],0);
          rgpType.SetFocus;
        end;
        genErlang: begin
          rgpType.ItemIndex := 4;
          with aGeneratorData as TErlangGeneratorData do
            begin
              txtMean.Text := FloatToStr(Mean);
              txtDispersion.Text := FloatToStr(Dispersion);
            end;
        end;
        genExponential: begin
          rgpType.ItemIndex := 5;
          with aGeneratorData as TExponentialGeneratorData do
            begin
              txtMean.Text := FloatToStr(Mean);
            end;
        end;
        genLinear: begin
          MessageDlg('sorry, this type is not supported yet',mtInformation,[mbOK],0);
          rgpType.SetFocus;
        end;
        genNormal: begin
          rgpType.ItemIndex := 7;
          with aGeneratorData as TNormalGeneratorData do
            begin
              txtMean.Text := FloatToStr(Mean);
              txtDispersion.Text := FloatToStr(Dispersion);
            end;
        end;
        genPoisson: begin
          rgpType.ItemIndex := 8;
          with aGeneratorData as TPoissonGeneratorData do
            begin
              txtMean.Text := FloatToStr(Mean);
            end;
        end;
        genUniform: begin
          rgpType.ItemIndex := 9;
          with aGeneratorData as TUniformGeneratorData do
            begin
              txtMinimum.Text := FloatToStr(From);
              txtMaximum.Text := FloatToStr(UniTo);
            end;
        end;
      end;
      enableTextsForIndex;
    end;
  ShowModal;
end;

procedure TfrmGenerator.txtConstantExit(Sender: TObject);
begin
  checkNumber(txtConstant);
end;

procedure TfrmGenerator.txtDispersionExit(Sender: TObject);
begin
  checkNumber(txtDispersion);
end;

procedure TfrmGenerator.txtMaximumExit(Sender: TObject);
begin
  checkValue(txtMaximum);
  if StrToFloat(txtMaximum.Text) < StrToFloat(txtMinimum.Text) then
    txtMinimum.Text := txtMaximum.Text;
end;

procedure TfrmGenerator.txtMeanChange(Sender: TObject);
begin
  checkNumber(txtMean);
end;

procedure TfrmGenerator.txtMinimumExit(Sender: TObject);
begin
   checkNumber(txtMinimum);
   if StrToFloat(txtMinimum.Text) > StrToFloat(txtMaximum.Text) then
     txtMaximum.Text := txtMinimum.Text;
end;

procedure TfrmGenerator.txtMultiplierExit(Sender: TObject);
begin
  checkValue(txtMultiplier);
end;

procedure TfrmGenerator.txtProbabilityExit(Sender: TObject);
begin
  checkNumber(txtProbability);
  if (StrToFloat(txtProbability.Text) < 0) or (StrToFloat(txtProbability.Text) > 1) then
    begin
      MessageDlg('probability out of range',mtError,[mbOK],0);
      txtProbability.SetFocus;
    end;
end;

procedure TfrmGenerator.txtValueExit(Sender: TObject);
begin
  checkNumber(txtValue);
end;

end.
